package com.web.onirigi.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.web.onirigi.model.RoomBean;
import com.web.onirigi.redis.RedisService;
import com.web.onirigi.service.HotelService;
import com.web.onirigi.util.BaseResponseObject;
import com.web.onirigi.util.BaseUtil;
import com.web.onirigi.util.ParseJsonToMap;

@RestController
@RequestMapping("hotelRest")
public class HotelRest {
   
	/**
     * 日志组件
     */
	private static final Logger logger =LoggerFactory.getLogger(HotelRest.class);
	
	@Autowired
	private HotelService   hotelServiceImpl;
	
	/**
	 * 提交表单
	 * @param json
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value ="submit",method={RequestMethod.POST,RequestMethod.GET})
	public BaseResponseObject  submit(@RequestBody String json) throws Exception{
		
		logger.info("提交内容" + json);
		BaseResponseObject  object =new BaseResponseObject();

		// RHtype 1  代表 的时候新增 ,2 代表的修改  ，3 代表的删除  4 代表的是查询  
		try {
			Map map =ParseJsonToMap.parseStringToMap(json);
			String type  =  map.get("RHtype").toString();
			map.remove("RHtype");
			RoomBean roomBean =JSON.parseObject(JSON.toJSONString(map), RoomBean.class);
			Map resultMap =new HashMap<>();
			if(BaseUtil.REST_TYPE_ADD.equals(type)){
				
				String roomId =roomBean.getRoomId() ;
				RoomBean  selectBean =new RoomBean() ;
				selectBean.setRoomId(roomId);
				List<RoomBean> beanList=hotelServiceImpl.selectRoomBeanList(roomBean);
				if(beanList.size() <= 0) {
					
					Integer  num=hotelServiceImpl.addRoomBean(roomBean);
					if( num > 0 ){
						object.setResponseCode(BaseUtil.SUCCESS_CODE);
						object.setResponseMessage(BaseUtil.HOSTL_SUCCESS_SUBMIT);
					}else{
						object.setResponseCode(BaseUtil.FAILED_CODE);
						object.setResponseMessage(BaseUtil.HOSTL_FAILED_SUBMIT);
					}
					
				}else {
					object.setResponseCode(BaseUtil.FAILED_CODE);
					object.setResponseMessage(BaseUtil.HOSTL_FAILED_ROOM_ID);
				}
				
				
				
			}else if(BaseUtil.REST_TYPE_UPDATE.equals(type)){
				String roomId =roomBean.getRoomId() ;
				RoomBean  selectBean =new RoomBean() ;
				selectBean.setRoomId(roomId);
				List<RoomBean> beanList=hotelServiceImpl.selectRoomBeanList(roomBean);
				
				if(beanList.size() <= 0) {
					hotelServiceImpl.updateRoomBean(roomBean);
					
				}else {
					object.setResponseCode(BaseUtil.FAILED_CODE);
					object.setResponseMessage(BaseUtil.HOSTL_FAILED_ROOM_ID);
				}
				
			}else if(BaseUtil.REST_TYPE_DELETE.equals(type)){
				hotelServiceImpl.deleteRoomeBean(roomBean);
			}else {
				List<RoomBean> beanList=hotelServiceImpl.selectRoomBeanList(roomBean);
				object.setResponseMessage(BaseUtil.HOSTL_SUCCESS_SUBMIT);
				map.put("result", new PageInfo<>(beanList) );
				object.setResponseData(map);
				
			}
			object.setResponseCode(BaseUtil.SUCCESS_CODE);
			object.setResponseMessage(BaseUtil.HOSTL_SUCCESS_SUBMIT);
			
		} catch (Exception e) {
			object.setResponseCode(BaseUtil.FAILED_CODE);
			object.setResponseMessage(BaseUtil.HOSTL_FAILED_OPERATE);
			logger.error("提交房间信息出错。。",e);
		}
		return object ;
	}
	
	

	
}
